<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 13-11-8
 * Time: 上午9:50
 * 显示票付通会员列表
 */
use \pft\Member\MemberAccount as Mem;
if($_SESSION['memberID']!=1 && !($_SESSION['sid']==1 && strpos($_SESSION['qx'],'mems'))){
    exit("不合法的身份访问！");
}
include '../common/autoload.php';
include COMMONPATH.'page.php';
$mem = new Mem($GLOBALS['le'],0, SoapInit::GetSoapInside());

$LD = array('供应商','分销商','资源方',5=>'普通会员',6=>'员工',7=>'集团');
$LS = array('正常','<span style="color:#F63">禁用</span>',
    '<span style="#F00">删除</span>','<span style="color:#00F">未审核</span>');

$currentPage =$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize    =$_REQUEST["pageSize"]?$_REQUEST["pageSize"]:10;
$offset     =($currentPage-1)*$pageSize;
if($_REQUEST['act']=='loadExcel'){
    $offset = 0;
    $pageSize = 99999;
}
$params = array(
    'dtype'=> '0,1',
    'limit'=> $offset,
    'offset'=> $pageSize,
);
$saleMan = array(
    1=>'薛小妹',
    2=>'林静静',
    3=>'林荣卫',
    4=>'张彬',
    5=>'陈萍',
    6=>'叶长壮',
    7=>'李凤',
    8=>'邓玉萍',
    9=>'张全',
    10=>'王佳鑫',
    11=>'陈增祥',
    12=>'余杰',
    13=>'钟志海',
    14=>'张静',
);
if($_SERVER['REQUEST_METHOD']=='GET') {
    $st = intval($_GET['searchType']);
    switch($st) {
        case 1:
            $params['dname'] = safetxt($_GET['squery']);
            break;
        case 2:
            $params['cname'] = safetxt($_GET['squery']);
            break;
        case 3:
            $params['mobile'] = safetxt($_GET['squery']);
            break;
        case 4:
            $params['alipay'] = safetxt($_GET['squery']);
            break;
        case 5:
            $params['id'] = intval($_GET['squery']);
            break;
        case 6:
            $params['dcode'] = safetxt($_GET['squery']);
            break;
        case 7:
            $params['account'] = safetxt($_GET['squery']);
            break;
        default:break;
    }

    if($_GET['dtype']>-1){
        $params['dtype'] = intval($_GET['dtype']);
    }
    if(isset($_GET['status'])){
        $params['status'] = intval($_GET['status']);
    }
    $params['bt1'] = safetxt($_GET['btime1']);
    $params['et1'] = safetxt($_GET['etime1']);
	$params['et1'].=' 23:59:59';
}
if(isset($_POST['gid']) && !empty($_POST['mid'])) {
    $son_id_type_L = array(0=>3, 1=>0);//供应商、分销商
//    print_r($_POST);exit;
    foreach($_POST['mid'] as $mid) {
        $mdata = explode('|', $mid);
        $id = $mdata[0];
        $son_id_type = $son_id_type_L[$mdata[1]];
        $ret = $mem->createRelationship($_POST['gid'], $id, $son_id_type, 1);
        //TODO:如果集团添加供应商，则默认将该供应商的所有分销商与集团建立关系
        if($son_id_type==3) {
            $fxs = $mem->gemMemRShip($id);
            foreach($fxs as $fx) {
                $ret2 = $mem->createRelationship($_POST['gid'], $fx['son_id'], 0, 1);
                if($ret2['status']!='ok') {
                    echo "<script>alert('{$ret['msg']}');</script>";
                    exit;
                }
            }
        }
        //var_export($ret);
        if($ret['status']!='ok') {
            echo "<script>alert('{$ret['msg']}');</script>";
            exit;
        }
    }
    echo '<script>alert("添加成功！");</script>';
    exit;
}

try{
    $mem_list = $mem->getMemList($params, false);
    $total_rows = $mem->getMemList($params, true);
//    print_r($mem_list);
} catch(Exception $e) {
    echo $e->getMessage();
}

$ids = $alliance = array();
foreach($mem_list as $k=>$ct){
    $ids[]=$ct['id'];
}
if(count($ids)){
    $sql="SELECT id,fid FROM pft_distributor_union_SE WHERE fid IN (".implode(',',$ids).")";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $alliance[]=$row['fid'];
    }
}
// print_r($alliance);
// print_r($mem_list);
if($_REQUEST['act']=='loadExcel'){
    $filename=date('YmdHis').'会员列表';
	// $Excel[0]['a']='编号';
	$Excel[0]['b']='身份';
	$Excel[0]['c']='企业名';
	$Excel[0]['d']='账号';
	// $Excel[0]['e']='接口编码';
	$Excel[0]['f']='联系人';
	$Excel[0]['g']='手机';
	$Excel[0]['h']='账户余额';
	$Excel[0]['i']='支付宝账号';
	$Excel[0]['j']='状态';
	$Excel[0]['k']='创建时间';
	$i=1;
	foreach($mem_list as $data){
		//print_r($row);
		$n++;
		// $Excel[$n]['a']=$data['id'];
		$Excel[$n]['b']=$LD[$data['dtype']];
		$Excel[$n]['c']=$data['dname'];
		$Excel[$n]['d']=$data['account'];
        // $Excel[$n]['e']='';
        $Excel[$n]['f']=$data['cname'];
        $Excel[$n]['g']=$data['mobile'];
        $dname = urlencode($data['dname']);
        $money = $mem->ShowBalance($data['id'])/100;
            
        $Excel[$n]['h']=$money;
        $Excel[$n]['i']=$data['alipay'];
        $Excel[$n]['j']=$LS[$data['status']];
        $Excel[$n]['k']=$data['creattime'];
	}
	$xls = new SimpleExcel('UTF-8', true,'ml');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
	exit;
}
$page_title = '票付通平台会员列表';
//include '../common/header.html';
include 'mem.tpl';
//include '../common/footer.tpl';